package LeetCode;

import java.util.List;
import java.util.Stack;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}15:50
 */
public class E12 {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        ListNode result = new E12().reverseList(head);
        while (result!=null){
            System.out.println(result.val);
            result = result.next;
        }
    }

    public ListNode reverseList(ListNode head) {
        if (head==null){
            return null;
        }
        ListNode temp = head;
        Stack<Integer> index = new Stack<>();
        while (temp!=null){
            index.push(temp.val);
            temp = temp.next;
        }
        temp = head;
        while (temp!=null){
            temp.val=index.pop();
            temp = temp.next;
        }
        return head;
    }
}
